CaptureZone_01 = ZONE:New("CaptureZone-1")
ZoneCaptureCoalition_01 = ZONE_CAPTURE_COALITION:New(CaptureZone_01,
SIDE.RED)

ZoneCaptureCoalition_01:__Guard(1)

ZoneCaptureCoalition_01:Start(10, 30)

local ZoneName_01 = "Guda"
local ZoneFlag_01 = false

-- enter guarded function
function ZoneCaptureCoalition_01:OnEnterGuarded( From, Event, To )
    
    if From ~= To then
        local coalition = self:GetCoalition()
        if coalition == SIDE.RED then
            SendMessageAll("Enemy controlled " .. tostring(ZoneName_01)) -- report zone01 is captured by red
            CLIENTS_GUDA:ForEachClient(function ( client )
                local name = client.ClientName
                USERFLAG:New(name):Set(0) -- ensable
            end)
            if RED_BASE_01:GetFirstAliveGroup() == nil then
                RED_BASE_01:Spawn()
            end -- if redbase is destroyed, then spawn a new one.
        elseif coalition == SIDE.BLUE then
            SendMessageAll("Our forces is taking control " .. tostring(ZoneName_01)) -- report zone01 is captured by blue
            RED_BASE_ATTACK_01:SpawnScheduleStart() -- spawn red groups attack zone01
            ZoneFlag_01 = true
            if BLUE_BASE_01:GetFirstAliveGroup() == nil then -- spawn blue base if there is no blue base in zone01
                BLUE_BASE_01:Spawn()
            end
        end
    end
end

-- enter empty function
function ZoneCaptureCoalition_01:OnEnterEmpty()
    if ZoneFlag_01 == false then -- if enemies are destroyed in zone01
        BLUE_BASE_01:Spawn()
    else -- if blue are destroyed in zone01
        RED_BASE_01:Spawn()
    end
end


function ZoneCaptureCoalition_01:OnEnterAttacked(From, Event, To)
    local coalition = self:GetCoalition()
    if coalition == SIDE.BLUE then -- BLUE Coalition is under attack in Zone01
        SendMessageAll(tostring(ZoneName_01) .. " is under attack!")
    else
        SendMessageAll("We're attacking" .. tostring(ZoneName_01))
    end
end

